/**
*
*  Sans mémoire d'activation et avec abort
*  Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN
* see <a href="{@docRoot}/Documentation/task.html"> for more information</a>
* 
* @author: Richard
* @version 3.2
*/

package storm.Tasks;

import java.util.Iterator;

import log.TaskTrace;
import storm.EvtContext;
import storm.Timer;
import storm.Tasks.Task.Activation;
/**
 * 
 * @author richard
 *
 */
public class PTask_WAM_A extends PTask {
	int compteurActivation=0;
	/**
	 * 
	 */
	public void controleDealine(){
		super.controleDealine();
		
	}
	/**
	 * 
	 */
	/**
	 * 
	 */
	public void Abort(){
		compteurActivation = compteurActivation-1;
		
		super.Abort();
		this.setNormalEnd(false);
		if (this.compteurActivation > 0 ) { 
			this.raiseUnBlock(this);
		    Timer timer = new Timer("resetNormalEnd",1,this);
			Kernel.addTimer(timer);
		}
	}

	public void resetNormalEnd(){
		if (!this.isNormalEnd() ) { 
			this.setNormalEnd(true);
			this.initRET();
		}
	}

	public void rearmTimer(){
		super.rearmTimer();
		Activation B = new Activation(this.Kernel.getTime()+1);
		this.ActivationFIFO.addLast(B);
		compteurActivation = compteurActivation+1;
	    Timer timer = new Timer("resetNormalEnd",1,this);
		Kernel.addTimer(timer);
		Timer timerD = new Timer("controleDealine", (int)this.deadline,this);
		if (this.isEverRunning())Kernel.addTimer(timerD);
		
		
	}
	/**
	 * 
	 */
	public void inRunning(){
		/**
		 * begin
		 */
		if(  (this.ET ==1 ) && (this.getRET()>=0 )) {
			Iterator I= this.ActivationFIFO.iterator();
			while (I.hasNext()) {
				Activation A = (Activation) I.next();
				if (A.getDeb_exec()< 0 ){
					A.setDeb_exec(this.Kernel.getTime());
					break;
				}
			}
		}
		/**
		 * end
		 */
		if(  (this.ET > 0 ) && (this.getRET()== 0)) {
			Iterator I= this.ActivationFIFO.iterator();
			while (I.hasNext()) {
				Activation A = (Activation) I.next();
				if (A.getEnd_exec()< 0 ){
					A.setEnd_exec(this.Kernel.getTime());
					break;
				}
				}
		}
		super.inRunning();
	}
	/**
	 * 
	 */
	public void onTick(){
		super.onTick();
		if (this.isDeadline) {
			Iterator I= this.ActivationFIFO.iterator();
			while (I.hasNext()) {
				Activation A = (Activation) I.next();
				if (A.getDeadline()<0) {
					A.setDeadline(this.Kernel.getTime()+1);
					/**
					 * miss deadline
					 */
					if (A.deb_exec<0) this.raiseBlock(this);
					if ((A.end_exec<0)&&(!A.loseActivation)) {
						this.Kernel.getLogs().addTrace(new TaskTrace(this.Kernel.getTime(),this, TaskTrace.Type.ABORT,A.getDate()));
						A.setEnd_exec(this.Kernel.getTime());
						A.setAbort(true);
						/**
						 * Check si je run ou pas 
						 */
						if (this.everRunning) this.raiseBlock(this);
						if (this.isIsrunning()) {
							Timer timer = new Timer("resetRun", 1 ,this);
							Kernel.addTimer(timer);
						}
						else {
							this.setEverRunning(false);
						}
						this.Abort();
					}
 					break;
				}
			}
			this.isDeadline = false;
		}
	}

	/**
	 * 
	 */
	public void onActivate(EvtContext c) {
		super.onActivate(c);
		compteurActivation = compteurActivation+1;
	}
	/**
	 * 
	 */
	public void finished(){
		compteurActivation = compteurActivation-1;
		if (this.compteurActivation > 0 ) this.raiseUnBlock(this);
		super.finished();
	}
}
